package com.cn.web.controller.TechnologicalStandard;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.pojo.TechnologicalStandard.*;
import com.cn.bean.pojo.bc.User;
import com.cn.bean.vo.Equipment.BusinessDimensionConfiguration;
import com.cn.config.config.Result;
import com.cn.service.server.Equipment.BusinessDimensionConfigurationService;
import com.cn.service.server.TechnologicalStandard.GrindingProcessStandardMainService;
import com.cn.service.server.bc.MaterialServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/Gri")
@Transactional
public class GrindingProcessStandardMainController {

    private final GrindingProcessStandardMainService grindingProcessStandardMainService;
    private final MaterialServer materialServer;

    @Autowired(required = false)
    public GrindingProcessStandardMainController(MaterialServer materialServer, GrindingProcessStandardMainService grindingProcessStandardMainService) {
        this.grindingProcessStandardMainService = grindingProcessStandardMainService;
        this.materialServer = materialServer;
    }


    @RequestMapping("/select/GrindingProcessStandardMain")
    @ResponseBody
    public List<GrindingProcessStandardMain> selectGrindingProcessStandardMain(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String equipmentStructure = jsonObject.getString("equipmentStructure");
        equipmentStructure = (equipmentStructure != null) ? equipmentStructure : "";
        String structureName = jsonObject.getString("structureName");
        String name = jsonObject.getString("name");
        String standardType = jsonObject.getString("standardType");
        String procedures = jsonObject.getString("procedures");
        structureName = (structureName != null) ? structureName : "";
        name = (name != null) ? name : "";
        standardType = (standardType != null) ? standardType : "";
        procedures = (procedures != null) ? procedures : "";
        List<GrindingProcessStandardMain> grindingProcessStandardMains = grindingProcessStandardMainService.selectGrindingProcessStandardMain(page, limit, equipmentStructure, structureName, name, standardType, procedures);
        for (GrindingProcessStandardMain grindingProcessStandardMain : grindingProcessStandardMains) {
            if (grindingProcessStandardMain.getState() == 0) {
                grindingProcessStandardMain.setStateName("待审核");

            } else {
                grindingProcessStandardMain.setStateName("已审核");
            }
            if (grindingProcessStandardMain.getStandardType().equals("0")) {
                grindingProcessStandardMain.setStandardType("通用标准");
            } else {
                grindingProcessStandardMain.setStandardType("专用标准");
            }
            if (grindingProcessStandardMain.getType().equals("0")) {
                grindingProcessStandardMain.setType("磨削标准");
            } else if (grindingProcessStandardMain.getType().equals("1")) {
                grindingProcessStandardMain.setType("安装标准");
            } else {
                grindingProcessStandardMain.setType("拆卸标准");
            }
            switch (grindingProcessStandardMain.getProcessProperty()) {
                case 0: {
                    grindingProcessStandardMain.setProcessPropertyName("——————");
                    break;
                }
                case 1: {
                    grindingProcessStandardMain.setProcessPropertyName("压延成品");
                    break;
                }
                case 2: {
                    grindingProcessStandardMain.setProcessPropertyName("压延半成品");
                    break;
                }
                case 5: {
                    grindingProcessStandardMain.setProcessPropertyName("成品,半成品");
                    break;
                }
            }

            switch (grindingProcessStandardMain.getProcedures()) {
                case 0: {
                    grindingProcessStandardMain.setProceduresName("轧机");
                    break;
                }
                case 1: {
                    grindingProcessStandardMain.setProceduresName("退火");
                    break;
                }
                case 2: {
                    grindingProcessStandardMain.setProceduresName("拉矫");
                    break;
                }
                case 3: {
                    grindingProcessStandardMain.setProceduresName("分条");
                    break;
                }
                case 4: {
                    grindingProcessStandardMain.setProceduresName("清洗线");
                    break;
                }
                case 5: {
                    grindingProcessStandardMain.setProceduresName("破带");
                    break;
                }
                case 6: {
                    grindingProcessStandardMain.setProceduresName("大分条");
                    break;
                }
                case 7: {
                    grindingProcessStandardMain.setProceduresName("去应力");
                    break;
                }
                case 8: {
                    grindingProcessStandardMain.setProceduresName("贴膜");
                    break;
                }
                case 9: {
                    grindingProcessStandardMain.setProceduresName("研磨");
                    break;
                }
            }

        }
        return grindingProcessStandardMains;
    }

    @RequestMapping("/select/GrindingProcessStandardMain/All")
    @ResponseBody
    public List<GrindingProcessStandardMain> selectGrindingProcessStandardMainAll(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String equipmentStructure = jsonObject.getString("equipmentStructure");
        equipmentStructure = (equipmentStructure != null) ? equipmentStructure : "";
        String structureName = jsonObject.getString("structureName");
        String name = jsonObject.getString("name");
        String standardType = jsonObject.getString("standardType");
        String procedures = jsonObject.getString("procedures");
        structureName = (structureName != null) ? structureName : "";
        name = (name != null) ? name : "";
        standardType = (standardType != null) ? standardType : "";
        procedures = (procedures != null) ? procedures : "";
        List<GrindingProcessStandardMain> grindingProcessStandardMains = grindingProcessStandardMainService.selectGrindingProcessStandardMainAll(page, limit, equipmentStructure, structureName, name, standardType, procedures);
        for (GrindingProcessStandardMain grindingProcessStandardMain : grindingProcessStandardMains) {
            if (grindingProcessStandardMain.getState() == 0) {
                grindingProcessStandardMain.setStateName("待审核");

            } else {
                grindingProcessStandardMain.setStateName("已审核");
            }
            if (grindingProcessStandardMain.getStandardType().equals("0")) {
                grindingProcessStandardMain.setStandardType("通用标准");
            } else {
                grindingProcessStandardMain.setStandardType("专用标准");
            }
            if (grindingProcessStandardMain.getType().equals("0")) {
                grindingProcessStandardMain.setType("磨削标准");
            } else if (grindingProcessStandardMain.getType().equals("1")) {
                grindingProcessStandardMain.setType("安装标准");
            } else {
                grindingProcessStandardMain.setType("拆卸标准");
            }
            switch (grindingProcessStandardMain.getProcessProperty()) {
                case 0: {
                    grindingProcessStandardMain.setProcessPropertyName("——————");
                    break;
                }
                case 1: {
                    grindingProcessStandardMain.setProcessPropertyName("压延成品");
                    break;
                }
                case 2: {
                    grindingProcessStandardMain.setProcessPropertyName("压延半成品");
                    break;
                }
                case 5: {
                    grindingProcessStandardMain.setProcessPropertyName("成品,半成品");
                    break;
                }
            }

            switch (grindingProcessStandardMain.getProcedures()) {
                case 0: {
                    grindingProcessStandardMain.setProceduresName("轧机");
                    break;
                }
                case 1: {
                    grindingProcessStandardMain.setProceduresName("退火");
                    break;
                }
                case 2: {
                    grindingProcessStandardMain.setProceduresName("拉矫");
                    break;
                }
                case 3: {
                    grindingProcessStandardMain.setProceduresName("分条");
                    break;
                }
                case 4: {
                    grindingProcessStandardMain.setProceduresName("清洗线");
                    break;
                }
                case 5: {
                    grindingProcessStandardMain.setProceduresName("破带");
                    break;
                }
                case 6: {
                    grindingProcessStandardMain.setProceduresName("大分条");
                    break;
                }
                case 7: {
                    grindingProcessStandardMain.setProceduresName("去应力");
                    break;
                }
                case 8: {
                    grindingProcessStandardMain.setProceduresName("贴膜");
                    break;
                }
                case 9: {
                    grindingProcessStandardMain.setProceduresName("研磨");
                    break;
                }
            }

        }
        return grindingProcessStandardMains;
    }

    @RequestMapping("/add/GrindingProcessStandardMain")
    @ResponseBody
    public Result addGrindingProcessStandardMain(@RequestBody JSONObject jsonObject) {
        String userName = jsonObject.getString("userName");
        String creationTime = jsonObject.getString("creationTime");
        creationTime = creationTime.replace("/", "-");
        GrindingProcessStandardMain formMainTable = jsonObject.getObject("formMainTable", GrindingProcessStandardMain.class);
        List<User> users = materialServer.selectUser(userName);
        String name = users.get(0).getName();
        formMainTable.setCreationTime(creationTime);
        formMainTable.setCreator(name);
        formMainTable.setCreatorID(userName);
        SpecializedGrindingTeamMain formSubTable11 = jsonObject.getObject("formSubTable11", SpecializedGrindingTeamMain.class);
        //技术标准
        JSONArray tableDate1Lsit = jsonObject.getJSONArray("tableDate1Lsit");
        List<TechnicalStandard> technicalStandardList = tableDate1Lsit.toJavaList(TechnicalStandard.class);
        //磨削道次
        JSONArray tableDate2Lsit = jsonObject.getJSONArray("tableDate2Lsit");
        List<GrindingPass> grindingPassList = tableDate2Lsit.toJavaList(GrindingPass.class);
        //更换类别
        JSONArray tableDate3Lsit = jsonObject.getJSONArray("tableDate3Lsit");
        List<ReplacementCategory> replacementCategoryList = tableDate3Lsit.toJavaList(ReplacementCategory.class);
        //磨削耗用人工
        JSONArray tableDate5Lsit = jsonObject.getJSONArray("tableDate5Lsit");
        List<GrindingConsumptionLabor> grindingConsumptionLaborList = tableDate5Lsit.toJavaList(GrindingConsumptionLabor.class);
        //磨削耗用辅助材料表
        JSONArray tableDate6Lsit = jsonObject.getJSONArray("tableDate6Lsit");
        List<GrindingConsumptionAuxiliaryMaterials> grindingConsumptionAuxiliaryMaterialsList = tableDate6Lsit.toJavaList(GrindingConsumptionAuxiliaryMaterials.class);
        //磨削（自制）耗用费用项目
        JSONArray tableDate7Lsit = jsonObject.getJSONArray("tableDate7Lsit");
        List<GrindingInhouseConsumptionExpenseItem> grindingInhouseConsumptionExpenseItemList = tableDate7Lsit.toJavaList(GrindingInhouseConsumptionExpenseItem.class);
        //磨削工步设置表
        JSONArray tableDate8Lsit = jsonObject.getJSONArray("tableDate8Lsit");
        List<GrindingProcessStepSetting> grindingProcessStepSettingList = tableDate8Lsit.toJavaList(GrindingProcessStepSetting.class);
        //磨削（委外）耗用费用项目
        JSONArray tableDate9Lsit = jsonObject.getJSONArray("tableDate9Lsit");
        List<GrindingOutsourcedConsumptionExpenseItem> grindingOutsourcedConsumptionExpenseItemList = tableDate9Lsit.toJavaList(GrindingOutsourcedConsumptionExpenseItem.class);
        //专用磨削设备设定
        JSONArray tableDate10Lsit = jsonObject.getJSONArray("tableDate10Lsit");
        List<SpecializedGrindingEquipmentSetup> SpecializedGrindingEquipmentSetup = tableDate10Lsit.toJavaList(SpecializedGrindingEquipmentSetup.class);
        //专用磨削班组子表
        JSONArray tableDate11Lsit = jsonObject.getJSONArray("tableDate11Lsit");
        List<SpecializedGrindingTeamSub> specializedGrindingTeamSubList = tableDate11Lsit.toJavaList(SpecializedGrindingTeamSub.class);

        return grindingProcessStandardMainService.addGrindingProcessStandardMain(formMainTable, grindingConsumptionAuxiliaryMaterialsList, grindingConsumptionLaborList, grindingInhouseConsumptionExpenseItemList, grindingOutsourcedConsumptionExpenseItemList, grindingPassList, grindingProcessStepSettingList, replacementCategoryList, SpecializedGrindingEquipmentSetup, formSubTable11, specializedGrindingTeamSubList, technicalStandardList);
    }

    @RequestMapping("/delete/GrindingProcessStandardMain")
    @ResponseBody
    public Result deleteGrindingProcessStandardMain(@RequestBody JSONObject jsonObject) {
        JSONArray deleteIdList = jsonObject.getJSONArray("deleteIdList");
        List<GrindingProcessStandardMain> SpecializedGrindingEquipmentSetup = deleteIdList.toJavaList(GrindingProcessStandardMain.class);

        return grindingProcessStandardMainService.deleteGrindingProcessStandardMain(SpecializedGrindingEquipmentSetup);
    }

    @RequestMapping("/select/GrindingProcessStandard/MainId")
    @ResponseBody
    public List<GrindingProcessStandardMain> selectGrindingProcessStandardMainId(@RequestBody JSONObject jsonObject) {
        Integer id = jsonObject.getInteger("id");

        return grindingProcessStandardMainService.selectGrindingProcessStandardMainId(id);
    }

    @RequestMapping("/update/GrindingProcessStandardMain")
    @ResponseBody
    public Result updateGrindingProcessStandardMain(@RequestBody JSONObject jsonObject) {
        String userName = jsonObject.getString("userName");
        String creationTime = jsonObject.getString("creationTime");
        creationTime = creationTime.replace("/", "-");
        GrindingProcessStandardMain formMainTable = jsonObject.getObject("formMainTable", GrindingProcessStandardMain.class);
        List<User> users = materialServer.selectUser(userName);
        String name = users.get(0).getName();
        formMainTable.setModifierID(userName);
        formMainTable.setModifier(name);
        formMainTable.setModificationTime(creationTime);
//        formMainTable.setCreationTime(creationTime);
//        formMainTable.setCreator(name);
//        formMainTable.setCreatorID(userName);
        SpecializedGrindingTeamMain formSubTable11 = jsonObject.getObject("formSubTable11", SpecializedGrindingTeamMain.class);
        //技术标准
        JSONArray tableDate1Lsit = jsonObject.getJSONArray("tableDate1Lsit");
        List<TechnicalStandard> technicalStandardList = tableDate1Lsit.toJavaList(TechnicalStandard.class);
        //磨削道次
        JSONArray tableDate2Lsit = jsonObject.getJSONArray("tableDate2Lsit");
        List<GrindingPass> grindingPassList = tableDate2Lsit.toJavaList(GrindingPass.class);
        //更换类别
        JSONArray tableDate3Lsit = jsonObject.getJSONArray("tableDate3Lsit");
        List<ReplacementCategory> replacementCategoryList = tableDate3Lsit.toJavaList(ReplacementCategory.class);
        //磨削耗用人工
        JSONArray tableDate5Lsit = jsonObject.getJSONArray("tableDate5Lsit");
        List<GrindingConsumptionLabor> grindingConsumptionLaborList = tableDate5Lsit.toJavaList(GrindingConsumptionLabor.class);
        //磨削耗用辅助材料表
        JSONArray tableDate6Lsit = jsonObject.getJSONArray("tableDate6Lsit");
        List<GrindingConsumptionAuxiliaryMaterials> grindingConsumptionAuxiliaryMaterialsList = tableDate6Lsit.toJavaList(GrindingConsumptionAuxiliaryMaterials.class);
        //磨削（自制）耗用费用项目
        JSONArray tableDate7Lsit = jsonObject.getJSONArray("tableDate7Lsit");
        List<GrindingInhouseConsumptionExpenseItem> grindingInhouseConsumptionExpenseItemList = tableDate7Lsit.toJavaList(GrindingInhouseConsumptionExpenseItem.class);
        //磨削工步设置表
        JSONArray tableDate8Lsit = jsonObject.getJSONArray("tableDate8Lsit");
        List<GrindingProcessStepSetting> grindingProcessStepSettingList = tableDate8Lsit.toJavaList(GrindingProcessStepSetting.class);
        //磨削（委外）耗用费用项目
        JSONArray tableDate9Lsit = jsonObject.getJSONArray("tableDate9Lsit");
        List<GrindingOutsourcedConsumptionExpenseItem> grindingOutsourcedConsumptionExpenseItemList = tableDate9Lsit.toJavaList(GrindingOutsourcedConsumptionExpenseItem.class);
        //专用磨削设备设定
        JSONArray tableDate10Lsit = jsonObject.getJSONArray("tableDate10Lsit");
        List<SpecializedGrindingEquipmentSetup> SpecializedGrindingEquipmentSetup = tableDate10Lsit.toJavaList(SpecializedGrindingEquipmentSetup.class);
        //专用磨削班组子表
        JSONArray tableDate11Lsit = jsonObject.getJSONArray("tableDate11Lsit");
        List<SpecializedGrindingTeamSub> specializedGrindingTeamSubList = tableDate11Lsit.toJavaList(SpecializedGrindingTeamSub.class);


        return grindingProcessStandardMainService.updateGrindingProcessStandardMain(formMainTable, grindingConsumptionAuxiliaryMaterialsList, grindingConsumptionLaborList, grindingInhouseConsumptionExpenseItemList, grindingOutsourcedConsumptionExpenseItemList, grindingPassList, grindingProcessStepSettingList, replacementCategoryList, SpecializedGrindingEquipmentSetup, formSubTable11, specializedGrindingTeamSubList, technicalStandardList);
    }

    @RequestMapping("/update/Sh/GrindingProcessStandardMain")
    @ResponseBody
    public Result updateShGrindingProcessStandardMain(@RequestBody JSONObject jsonObject) {
        JSONArray deleteIdList = jsonObject.getJSONArray("deleteIdList");
        List<GrindingProcessStandardMain> SpecializedGrindingEquipmentSetup = deleteIdList.toJavaList(GrindingProcessStandardMain.class);

        String userName = jsonObject.getString("userName");
        String creationTime = jsonObject.getString("creationTime");
        creationTime = creationTime.replace("/", "-");
        List<User> users = materialServer.selectUser(userName);
        String name = users.get(0).getName();
        Integer state = jsonObject.getInteger("state");


        return grindingProcessStandardMainService.updateShGrindingProcessStandardMain(SpecializedGrindingEquipmentSetup, state, name, userName, creationTime);
    }

    @RequestMapping("/update/selectGmModel")
    @ResponseBody
    public List<GrindingProcessStandardMain> selectGmModel(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String model = jsonObject.getString("input1");
        System.out.println("aaaaaaaaaaaaaaaaaa："+model);
        return grindingProcessStandardMainService.selectGmModel(page, limit, model);
    }
}
